String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}

function createHiddenField(name, id, value){
      var hiddenField = document.createElement("input");
      hiddenField.setAttribute("type", "hidden");
      hiddenField.setAttribute("name", name+"["+id+"]");
      hiddenField.setAttribute("id", name+"_"+id);
      hiddenField.setAttribute("value", value);
      
      $("#topics_selection")[0].appendChild(hiddenField);
      return hiddenField;
}

function submit_form(object_name)
{
  $("input[id^='adviser_']").attr("disabled", false);
  $("input[id^='owner_']").attr("disabled", false);
	
  var topics = new Array();
  jQuery.tree.plugins.checkbox.get_checked().each(function(){
		var v = jQuery.find_topic(jQuery(this).attr('id'));
		if (v && ! v.is_root())
		{
			topics[v.id] = createHiddenField(object_name, v.id, v.weeks_field().val());
		}  	
  });
  
  jQuery.tree.plugins.checkbox.get_undeterminded().each(function(){
		var v = jQuery.find_topic(jQuery(this).attr('id'));
		if (v && ! v.is_root())
		{
			topics[v.id] = createHiddenField(object_name, c.children("input").val(), v.id);
		}  	
  });
  
}

function after_reload()
{
  jQuery.tree.plugins.checkbox.get_checked().each(function(){
		var v = jQuery.find_topic(jQuery(this).attr('id'));
		if (v && ! v.is_root() && v.comment_area().is(':visible'))
		{
			jQuery.toggle_comment(v.id);
		}  	
  });
}

function adviser_validate_cb(v, validate, topic_id)
{
	v.adviser_validate(validate, topic_id);
}

function adviser_validate_parent(v)
{
	var validate = true;
	
	v.topics().each(function(){
		var i = jQuery.find_topic(jQuery(this).attr('id'));
		if (i && !i.adviser_checkbox().is(':checked'))
		{
			validate = false;
		} 		
	});
	
	v.adviser_checkbox().attr("checked", validate).attr("disabled", validate);
	return validate;
}

function owner_validate_cb(v, validate, topic_id)
{
	v.owner_validate(validate, topic_id);
}

function owner_validate_parent(v)
{
	var validate = true;
	
	v.topics().each(function(){
		var i = jQuery.find_topic(jQuery(this).attr('id'));
		if (i && !i.owner_checkbox().is(':checked'))
		{
			validate = false;
		} 		
	});
	
	v.owner_checkbox().attr("checked", validate).attr("disabled", validate);
	return validate;
}

function Topic (id){
	this.id = id;
	this.tree_node = function(){ return $("#node_"+this.id) };
	this.comment_icon = function(){ return $("#img_comment_"+this.id) };
			
	// comment layer
	this.comment_layer = function(){ return $("#comment_layer_"+this.id) };
	this.adviser_checkbox = function(){ return $("#adviser_"+this.id) };
	this.adviser_validation_topic = function(){ return $("#adviser_validation_"+this.id) };
	this.owner_checkbox = function(){ return $("#owner_"+this.id) };
	this.owner_validation_topic = function(){ return $("#owner_validation_"+this.id) };
	this.comment_area = function(){ return $("#comment_"+this.id) };
			
	// weeks layer
	this.weeks_layer = function(){ return $("#weeks_layer_"+this.id) };
	this.weeks_field = function(){ return $("#w_"+this.id) };
	this.weeks_number = parseInt(this.weeks_field().val(),10); 
	
	this.is_root = function (){
		// I am an LI node, my parent is an UL. If my grand parent is no an LI... 
		// I'm a root node !!
		var v = this.tree_node();
		return (v.length <= 0 || !v.parent().parent().is('li'));
	}
	
	this.topics = function(){
		var v = this.tree_node();
		return (v.children('ul').length > 0 ? v.children('ul').children('li') : $("empty"));
	}
	
	this.children_topics = function(){
		var ctopics = new Array();
		this.topics().each(function(){
			ctopics[ctopics.length] = jQuery.find_topic(jQuery(this).attr('id'));
		});
		return ctopics;
	}
	
	this.parent = function() {
		if (this.is_root())
			return $("empty");
		else
			return jQuery(this.tree_node().parent().parent());
	}
	
	this.parent_topic = function(){
		return jQuery.find_topic(this.parent().attr('id'));
	}
	
	this.checked = function(){
		if (this.is_root()){
			// I am a step, and I haven't got any information about topic validation...
			// I let my children doing the job.
			this.topics().each(function(){
				var v = jQuery.find_topic(jQuery(this).attr('id'));
				if (v) 
					v.checked();
			});
		}else{
			// Display comment icon on current node and his children
			// we show comment layer on the current node.
			this.tree_node().find('img[id^="img_comment_"]').show().attr('src', 'images/arrow_down_blue.png');
			this.tree_node().find('div[id^="comment_layer"]').hide();
			this.comment_icon().attr('src', 'images/arrow_up_blue.png');
			this.comment_icon().show();
			this.comment_layer().show();
			this.weeks_layer().show().attr('style', 'display:inline;');
			this.topics().find('div[id^="weeks_layer_"]').show().attr('style', 'display:inline;');
			
			// make current node detail editable
			this.adviser_checkbox().attr("disabled", false);
			this.owner_checkbox().attr("disabled", false);
			this.comment_area().attr("readonly", false);
		}
	}
	
	this.unchecked = function(){
		this.comment_icon().hide();
		this.weeks_layer().hide();
		this.comment_layer().hide();
		this.adviser_checkbox().attr("disabled", false);
		this.owner_checkbox().attr("disabled", false);
		this.comment_area().attr("readonly", false);
		
		// simply hide all sub items
		this.topics().each(function(){
			var v = jQuery.find_topic(jQuery(this).attr('id'));
			if (v) {
				v.unchecked();
			}
		});
		
	}

	this.validate = function(checkbox, validation_topic, cb, cb_parent, validate, topic_id){
		if (validate !== true && validate !== false){
			validate = checkbox.is(":checked");
		}
		if (validate){
			if (topic_id){
				validation_topic.val(topic_id);
			}else{
				validation_topic.val("");
				topic_id = this.id
			}
		}else{
			validation_topic.val("");
		}
		
		var lck = this.adviser_validation_topic().val() != "" || this.owner_validation_topic().val() != "";
		checkbox.attr("checked", validate);
		checkbox.val(validate);
		checkbox.attr("disabled", validation_topic.val() != "");
		this.comment_area().attr("readonly", lck);

		this.topics().each(function(){
			var v = jQuery.find_topic(jQuery(this).attr('id'));
			if (v) {
				cb(v, validate, topic_id);
			}
		});
		
		if (this.parent_topic()){
			v= this.parent_topic();
			while (v && cb_parent(v)){
				v = v.parent_topic();
			}
		}
	}

	
	this.adviser_validate = function(validate, topic_id){
		this.validate(
			this.adviser_checkbox(), 
			this.adviser_validation_topic(), 
			adviser_validate_cb, 
			adviser_validate_parent, 
			validate, 
			topic_id
		);
	};
	
	this.owner_validate = function(validate, topic_id){
		this.validate(
			this.owner_checkbox(), 
			this.owner_validation_topic(), 
			owner_validate_cb, 
			owner_validate_parent, 
			validate, 
			topic_id
		);
	};
	
	this.week_changed=function(val,rChild,rParent){
		// if value > parent value, we update it
		val = parseInt (val,10);
		if (val != this.weeks_number){
			if (rParent !== false){
				if (this.parent_topic()){
					if (this.parent_topic().weeks_number <= val){
						this.parent_topic().week_changed(val,false, true);
					}
				}
			}
			
			if (rChild !== false){
				// we update all children that have the same weeks number
				var c = this.children_topics();
				for (var i=0 ; i < c.length ; i++){
					if (c[i].weeks_number >= this.weeks_number){
						c[i].week_changed(val, true, false);
					}
				} 
			}
		}
		
		this.weeks_number = val;
		this.weeks_field().val(this.weeks_number);
	}
	
	this.weeks_field().live("change", function(){
		var v = jQuery.find_topic('node_'+jQuery(this).attr('id').replace (/w_/, ""));
		if (v)
			v.week_changed(jQuery(this).val())
	});
	
	this.weeks_field().live("keydown", function(e){
		return Topic.mask.isAllowKeyPress(e, this);
	});
	
	this.weeks_field().live("keyup", function(e){
		return Topic.mask.getKeyPress(e, this);
	});
	
	this.weeks_field().live("blur", function(e){
		return Topic.mask.format(this.value);
	});

}

Topic.mask = new Mask("##", "number");

(function ($) {
	// jQuery plugin	
	$.topic_validation = {
		topic : new Array(),
		
		initialize : function(node){
			var v = jQuery(node);
			var id = v.attr('id').replace(/node_/,'');
			jQuery.topic_validation.topic[v.attr('id')] = new Topic(id);
			return node;
		}
	};
	
	$.fn.topic_validation = function () {
		return this.each(function() {
			jQuery.topic_validation.initialize(jQuery(this));
		});
		return this;
	};
	
	$.extend({ 
		find_topic : function(id){
			if (typeof id === "string")
				return jQuery.topic_validation.topic[id];
			else if (typeof id === "Object")
				return jQuery.topic_validation.topic[jQuery(id).attr('id')];
			else
				return undefined;
		},
		
		toggle_comment : function(id) {
			v = $.find_topic("node_"+id);
			if (v){
				if (v.comment_icon() && v.comment_icon().is(':visible')){
					if (v.comment_layer().is(':visible')){
						v.comment_layer().hide();
						v.comment_icon().attr('src', 'images/arrow_down_blue.png')
					}else{
						v.comment_layer().show();
						v.comment_icon().attr('src', 'images/arrow_up_blue.png')
					}
				}
			}
		}
	});
	
})(jQuery);